var pnum = "";
//初始化动车设施库
var initBulletFacility = function(pnum){
    var param = {
        page_num:pnum,
        page_size:"10"
    }
    $.ajax({
        url: base + "/projectCenter/businessdata/findBulletFacilityList",
        data: param,
        success: function (data, textStatus, jqXHR) {
            if(data.list != null){
                var pageCount = data.pager.pageCount;
                var dataObj ={
                    "list":data.list,
                    "dataTool":{
                        "title":{
                        	"crh_info_city":"城市"
                        	,"crh_info_department":"路局"
                            ,"crh_info_name":"运用所名称"
                            ,"crh_info_type":"性质"
                            ,"crh_info_size":"规模"
                            ,"crh_info_longitude":"经度"
                            ,"crh_info_latitude":"纬度"
                            ,"operation":"操作"
                        },
                        "titleCheckbox":false,
                        "btn":[{"event":"updateObj","name":"编辑"},
                               {"event":"showMap","name":"查看地图标记"},
                               {"event":"ssxxxx","name":"设施详细信息"},
                            {"event":"delObj","name":"删除"}]
                    }
                };
                showPageTable('view', dataObj,pnum,pageCount,"initBulletFacility");
            }

        },
        error: function () {
        }
    });
}

//取得表单信息
var initFormField = function(id){
	   var contentDiv;
	   $.ajax({
		   url:base+"/dataCenter/businessdata/affixFile/"+id,
		   data:{},
		   async: false,
		   success:function(data,status){
			   contentDiv = data;
		   }
	   });
	   return contentDiv;
}

//设施详细信息
var ssxxxx = function(obj){
	var id = obj.id;
    var content_div = initFormField(id);
	layer.open({
	  	title:'上传设备信息'
		,content:content_div
    	,area: ['1400px', '900px']
		,scrollbar: false
	  	,btn: ['保存', '取消']
		,yes: function(index,layero){ //确认
		 	var data_arr = $('form').serializeArray();
		 	var data_json = {};
		 	$.each(data_arr,function(index,value){
		 		var field_name = value.name;
		 		var field_value = value.value;
		 		data_json[field_name] = field_value;
		 	});
			$.post(base+"/dataCenter/businessdata/updateBulletFacilityInfo/"+id,data_json,
					function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
			});
			layer.close(index);
			return false;
		}
    });
	
	
	
}

//查看地图标记
var showMap = function(obj){
	var x_coord = obj.crh_info_latitude;
	var y_coord = obj.crh_info_longitude;
	//window.open(base + "/dataCenter/businessdata/showMapForBulletFacility/"+x_coord+"/"+y_coord);
    layer.open({
    	  type: 2,
    	  title: '地图显示',
    	  shadeClose: true,
    	  shade: 0.8,
    	  area: ['800px', '600px'],
    	  content: base + "/datacenter/businessdata/showMapForBulletFacility?x_coord="+x_coord+"&y_coord="+y_coord //iframe的url
    	}); 
	return false;
}

//添加动车资料
var addData = function(){
    var contentDiv = function(){
        var content;
        var data={
            "title":{
            "crh_info_name":"运用所名称"
            ,"crh_info_type":"性质"
            ,"crh_info_size":"规模"
            ,"crh_info_city":"城市"
            ,"crh_info_department":"路局"
            ,"crh_info_longitude":"经度"
            ,"crh_info_latitude":"纬度"
            },"text":{
                "crh_info_name":""
                ,"crh_info_type":""
                ,"crh_info_size":""
                ,"crh_info_city":""
                ,"crh_info_department":""
                ,"crh_info_longitude":""
                ,"crh_info_latitude":""
            },"input":{
                "crh_info_name":"text"
                ,"crh_info_type":"text"
                ,"crh_info_size":"text"
                ,"crh_info_city":"text"
                ,"crh_info_department":"text"
                ,"crh_info_longitude":"text"
                ,"crh_info_latitude":"text"
            },"input_init":{
            }
        };
        var getDiv =  dc_update.innerHTML;
        laytpl(getDiv).render(data, function(html){
            content = html;
        });
        return content;
    }
    layopen(contentDiv());
}

$("#addData").click(addData);


//编辑提出资料
var updateObj = function(obj){
    var contentDIV = function(){
        var content;
        var data = {
            "title":{
                "id":"主键"
                ,"crh_info_name":"运用所名称"
                ,"crh_info_type":"性质"
                ,"crh_info_size":"规模"
                ,"crh_info_city":"城市"
                ,"crh_info_department":"路局"
                ,"crh_info_longitude":"经度"
                ,"crh_info_latitude":"纬度"
            },"text":{
                "id":obj.id
                ,"crh_info_name":obj.crh_info_name
                ,"crh_info_type":obj.crh_info_type
                ,"crh_info_size":obj.crh_info_size
                ,"crh_info_city":obj.crh_info_city
                ,"crh_info_department":obj.crh_info_department
                ,"crh_info_longitude":obj.crh_info_longitude
                ,"crh_info_latitude":obj.crh_info_latitude
            },"input":{
                "id":"hidden"
                ,"crh_info_name":"text"
                ,"crh_info_type":"text"
                ,"crh_info_size":"text"
                ,"crh_info_city":"text"
                ,"crh_info_department":"text"
                ,"crh_info_longitude":"text"
                ,"crh_info_latitude":"text"
            },"input_init":{
            }
        };
        var getDiv = dc_update.innerHTML;
        laytpl(getDiv).render(data,function(html){
            content = html;
        });
        return content;
    }
    layopen(contentDIV());
}
//打开弹窗
var layopen = function(content){
    layer.open({
        title:'更新动车信息库'
        ,content: content
        ,area: ['800px', '600px']
        ,scrollbar: false
        ,btn: ['确认', '取消']
        ,yes: function(index,layero){ //确认
            var data = $(layero).find("form").serializeJSON();
            if(verification_data(data)){
                $.post(base+"/dataCenter/businessdata/updateBulletFacility",data,
                    function (data,status){
                        if(data!=""&&data!=null){
                            if(data.statusCode == 0){
                                layer.msg('更新成功', {icon: 1,time:2000});
                            }else{
                                layer.msg(data.message, {icon: 2,time:2000});
                            }
                        }else{
                            layer.msg('更新失败!', {icon: 2,time:2000});
                        }
                        initBulletFacility(pnum);
                    })
            }
        }
    });
}


//验证数据
var verification_data = function(data){

    if(data.crh_info_name== ""){
        var obj = $("input[name='crh_info_name']");
        obj.focus();
        layer.tips('请填写运用所名称', obj);
        return false;
    }
    if(data.crh_info_type == ""){
        var obj = $("input[name='crh_info_type']");
        obj.focus();
        layer.tips('请填写性质', obj);
        return false;
    }
    if(data.crh_info_size == ""){
        var obj = $("input[name='crh_info_size']");
        obj.focus();
        layer.tips('请填写规模', obj);
        return false;
    }
    if(data.crh_info_city == ""){
        var obj = $("input[name='crh_info_city']");
        obj.focus();
        layer.tips('请填写城市', obj);
        return false;
    }
    if(data.crh_info_department == ""){
        var obj = $("input[name='crh_info_department']");
        obj.focus();
        layer.tips('请填写路局', obj);
        return false;
    }
    if(data.crh_info_longitude == ""){
        var obj = $("input[name='crh_info_longitude']");
        obj.focus();
        layer.tips('请填写经度', obj);
        return false;
    }
    if(data.crh_info_latitude == ""){
        var obj = $("input[name='crh_info_latitude']");
        obj.focus();
        layer.tips('请填写纬度', obj);
        return false;
    }
    return true;
}

//删除动车信息
var delObj = function(obj){
    layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
        layer.close(index);
        var param = {id:obj.id}
        $.ajax({
            url:base+"/dataCenter/businessdata/delBulletFacilityById",
            data:param,
            success:function(data,status){
                if(data.statusCode == 0){
                    layer.msg(data.message, {
                        icon : 1
                    });
                }else{
                    layer.msg(data.message, {
                        icon : 5
                    });
                }
                initBulletFacility(pnum);
            }
        });
    });
}

var searchObj = function(num){
    var param = {
        page_num:num,
        page_size:"10",
        crh_info_name:$.trim($("#searchName").val())
    }
    $.ajax({
        url: base + "/projectCenter/businessdata/findBulletFacilityList",
        data: param,
        success: function (data, textStatus, jqXHR) {
            if(data != null){
                var pageCount = data.pager.pageCount;
                var dataObj ={
                    "list":data.list,
                    "dataTool":{
                        "title":{
                        	"crh_info_city":"城市"
                            ,"crh_info_department":"路局"
                            ,"crh_info_name":"运用所名称"
                            ,"crh_info_type":"性质"
                            ,"crh_info_size":"规模"
                            ,"crh_info_longitude":"经度"
                            ,"crh_info_latitude":"纬度"
                            ,"operation":"操作"
                        },
                        "titleCheckbox":false,
                        "btn":[{"event":"updateObj","name":"编辑"},
                            {"event":"delObj","name":"删除"}]
                    }
                };
                showPageTable('view', dataObj,num,pageCount,"searchObj");
            }
        },
        error: function () {
        }
    });
}

$(document).ready(function(){
    initBulletFacility(1);
});
